﻿// JScript File


function ChoiceInteraction(owner)
{
    this.owner = owner;
    var xmldom = CreateXmlDOM();
    this.Interact = function(event)
    {            
        var aEvent = event ? event : window.event;        
        var inp = aEvent.srcElement ? aEvent.srcElement : aEvent.target;
        if (this.owner.iit == IIT_CHOICEMULTIPLE)
        {                    
            if (inp.checked)
            {
                var valueNode = xmldom.createElement("value");
                
                if (!isMozilla) 
                    valueNode.text = inp.value;
                else
                    valueNode.textContent = inp.value;
                    
                var urNode = this.owner.xml.getElementsByTagName("userResponse")[0];
                urNode.appendChild(valueNode);
                
            }
            else
            {
                var urNode = this.owner.xml.getElementsByTagName("userResponse")[0];
                var userResponse = this.owner.xml.getElementsByTagName("userResponse")[0];
                var valueNodes = userResponse.getElementsByTagName("value");
                for (var i = 0; i < valueNodes.length; i++)
                {
                    if (!isMozilla ? (valueNodes[i].text == inp.value) : (valueNodes[i].textContent == inp.value))
                    {
                        urNode.removeChild(valueNodes[i]);
                        break;
                    }
                }
            }
        }
        else        //IIT_CHOICE
        {
            if (inp.checked)
            {
                var userResponse = this.owner.xml.getElementsByTagName("userResponse")[0];
                var valueNodes = userResponse.getElementsByTagName("value");
                
                var valueNode = null;                
                if (valueNodes.length > 0)
                    valueNode = valueNodes[0];
                
                if (valueNode == null)
                {
                    valueNode = xmldom.createElement("value");                    
                    userResponse.appendChild(valueNode);
                }                
                
                if (!isMozilla) 
                    valueNode.text = inp.value
                else
                    valueNode.textContent = inp.value
            }
            else
            {
                var userResponse = this.owner.xml.getElementsByTagName("userResponse")[0];
                var valueNode = userResponse.getElementsByTagName("value")[0];
                if (!isMozilla)                 
                    valueNode.text = "";
                else
                    valueNode.textContent = "";
            }     
        }
    }        
}
   
function ChoiceRendering(owner)
{
    this.owner = owner;
    
    this.Render = function()
    {
        try
        {
            var html = "";                        
            html += TransformNode(this.owner.xml, "xslt/ItemHeader.xsl.aspx");
            var itemBody = this.owner.xml.getElementsByTagName("itemBody")[0];            
            var p = itemBody.getElementsByTagName("p");                        
            if (p.length > 0)
                html += SerializeXmlNode(p[0]);
            html += TransformNode(this.owner.xml, "xslt/ChoiceInteraction.xsl");
            return html;
        }
        catch (e)
        {
            ShowMessage(ChoiceInteraction_QuestionRenderingError + (e.message == undefined ? e : e.message));
        }
    }
}
